/**
 * @file tal_group_ota.h
 * @brief This is tal_group_ota file
 * @version 1.0
 * @date 2022-08-15
 *
 * @copyright Copyright 2021-2031 Tuya Inc. All Rights Reserved.
 *
 */

#ifndef __TAL_GROUP_OTA_H__
#define __TAL_GROUP_OTA_H__

#include "tuya_cloud_types.h"
#include "tuya_error_code.h"

#ifdef __cplusplus
 "C" {
#endif

/***********************************************************************
 ********************* constant ( macro and enum ) *********************
 **********************************************************************/
#define OTA_ZIGBEE_MTU_SIZE         60
#define OTA_BLE_MASH_MTU_SIZE       10
typedef enum
{
    PROTOCAL_ZIGBEE = 0,
    PROTOCAL_BLE,
} GROUP_OTA_PROTOCOL_TYPE_E;

typedef enum{
    GROUP_OTA_FRAME_TYPE_UNICAST = 0,
    GROUP_OTA_FRAME_TYPE_BROADCAST,
} GROUP_OTA_FRAME_TYPE_E;

typedef enum{
    GROUP_OTA_RET_OK =0,
    GROUP_OTA_RET_INVALID_PARAM,      
    GROUP_OTA_RET_MALLOC_ERR,
    GROUP_OTA_RET_BUSY,
    GROUP_OTA_RET_INACTIVE,
    GROUP_OTA_RET_FUNC_NULL,
    GROUP_OTA_RET_FUNC_FAILED,
    GROUP_OTA_RET_CREAT_TIMER_FAILED,
    GROUP_OTA_RET_FRAME_TYPE_ERR,
    GROUP_OTA_RET_CHECK_SUM_ERR,
    GROUP_OTA_RET_CHECK_PID_ERR,
    GROUP_OTA_RET_CHECK_FW_SIZE_ERR,
    GROUP_OTA_RET_CHECK_FW_SIGN_TYPE_ERR,
    GROUP_OTA_RET_CHECK_FW_VERSION_ERR,
    GROUP_OTA_RET_CHECK_PROTOCOL_VERSION_ERR,
    GROUP_OTA_RET_CHECK_TARGET_VERSION_ERR,
    GROUP_OTA_RET_LOAD_LOST_SN_ERR,
    GROUP_OTA_RET_FRAME_LEN_ERR,
    GROUP_OTA_RET_SEND_DATA_ERR,
    GROUP_OTA_RET_FRAME_SEQ_ERR,
    GROUP_OTA_RET_FRAME_SEQ_DUPLICATED,
    GROUP_OTA_RET_FILEID_ERR,
    GROUP_OTA_RET_MANU_CODE_ERR,
    GROUP_OTA_RET_IMAGE_TYPE_ERR,
    GROUP_OTA_RET_MAGIC_ERR,
    GROUP_OTA_RET_FW_WRITE_ERR,
    GROUP_OTA_RET_CAPACITY_ERR,
    GROUP_OTA_RET_CAPACITY_UPDATE_ERR,

}GROUP_OTA_RET_E;

typedef OPERATE_RET (* FLASH_OP_WRITE_CB)(UINT32_T addr, CONST UCHAR_T *src, UINT32_T size, UINT32_T total_size);
//typedef OPERATE_RET (* FLASH_OP_READ_CB)(UINT32_T addr, UCHAR_T *dst, UINT32_T size);
typedef OPERATE_RET (* FLASH_OP_ERASE_CB)(UINT32_T addr, UINT32_T size);
typedef OPERATE_RET (* OTA_FIRMWARE_VALIDATE_CB)(UINT32_T size,UINT8_T version);
typedef OPERATE_RET (* OTA_EXECUTE_CB)(UINT8_T version);

/***********************************************************************
 ********************* struct ******************************************
 **********************************************************************/

typedef struct{
    FLASH_OP_ERASE_CB erase_func;
//    FLASH_OP_READ_CB read_func;
    FLASH_OP_WRITE_CB write_func;
    OTA_FIRMWARE_VALIDATE_CB fw_pre_validate_func;
    OTA_FIRMWARE_VALIDATE_CB fw_step_validate_func;
    OTA_EXECUTE_CB execute_func;

    UINT16_T manufacturer_Code;
    UINT16_T image_type;
    GROUP_OTA_PROTOCOL_TYPE_E protocol;
    UINT8_T mtu;
    UINT8_T app_version;
}GROUP_OTA_INIT_T;
/***********************************************************************
 ********************* variable ****************************************
 **********************************************************************/


/***********************************************************************
 ********************* function ****************************************
 **********************************************************************/

/**
 * @brief group ota initialization
 *
 * @param init
 * @return bool_t
 */
GROUP_OTA_RET_E tal_group_ota_init(GROUP_OTA_INIT_T *init);
/**
 * @brief group ota protocol frame data receive handle
 * 
 * @param type 
 * @param pdata 
 * @param len 
 * @return GROUP_OTA_RET_E 
 */
GROUP_OTA_RET_E tal_group_ota_receive(GROUP_OTA_FRAME_TYPE_E type,
                            UINT8_T *pdata,
                            UINT16_T len);
/**
 * @brief release all Resource overhead belongs to group ota module 
 *
 * @return VOID_T
 */
VOID_T tal_group_ota_stop(VOID_T);
#ifdef __cplusplus
}
#endif

#endif /* __TAL_GROUP_OTA_H__ */

